#include<stdio.h>
#include<stdlib.h>

int search2(int A[],int length,int t)
{
    int low=0,high=length-1,mid;
    while(low<high-1)
    {
        mid=(low+high)/2;
        if(A[mid]>=t)  high=mid;
        if(A[mid]<t)   low=mid;
    }
    if(A[low]==t)
        return low;
    else if(high>0 && A[high]==t)
        return high;
    else
        return -1;
}

int cmp(const void *a,const void *b)
{
    return (*(int*)a)>(*(int*)b)?1:-1;
}

int main()
{
    int a[10]={2, 3, 9, 54, 62, 58, 58, 65, 87, 5};
    qsort(a,10,sizeof(a[0]),cmp);
    int i;
    for(i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("=%d\n",search2(a,10,2));
    printf("=%d\n",search2(a,10,100));
    printf("=%d\n",search2(a,10,23));
    printf("=%d\n",search2(a,10,62));
    printf("=%d\n",search2(a,10,58));
}

